export const myVuethrottle = (app: any) => {
    // 全局节流指令
    app.directive('myThrottle', {
        mounted(el: any, binding: any) {
            const delay = binding.value?.delay || 500; 
            const handler = binding.value?.handler || binding.value; // 兼容直接传函数的情况
            let timer: any = null;
            let lastExecTime = 0; // 记录上一次执行时间

            el.addEventListener('click', function (this: any, ...args: any[]) {
                const now = Date.now();
                // 立即执行逻辑（时间戳法）
                if (now - lastExecTime >= delay) {
                    handler.apply(this, args); // 修正 this 指向当前元素
                    lastExecTime = now;
                } 
                // 定时器兜底（确保最后一次操作后仍会执行）
                else if (!timer) {
                    timer = setTimeout(() => {
                        handler.apply(this, args);
                        lastExecTime = Date.now();
                        timer = null;
                    }, delay - (now - lastExecTime));
                }
            });
        }
    })
}
   